home *** CD-ROM | disk | FTP | other *** search
/ Oh!X 2001 Spring / Oh!X 2001 Spring Special CD-ROM (Japan).7z / Oh!X 2001 Spring Special CD-ROM (Japan) (Track 1).bin / VC / Alpha / Alpha.cpp < prev    next >
C/C++ Source or Header  |  2000-08-11  |  8KB  |  268 lines

  1. // Alpha.cpp : アプリケーション用のエントリ ポイントの定義
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "AlphaWindow.h"
  6.  
  7. #define APPNAME "AlphaWindow"
  8. static char szAppName[] = APPNAME;
  9. static char szTitle[]   = APPNAME;
  10.  
  11. #define MM_QUIT WM_USER+1
  12.  
  13. AlphaWindow *palphaWnd;
  14.  
  15. int APIENTRY WinMain(HINSTANCE hInstance,
  16.                      HINSTANCE hPrevInstance,
  17.                      LPSTR     lpCmdLine,
  18.                      int       nCmdShow )
  19. {
  20.      // TODO: この位置にコードを記述してください。
  21.     if( !InitInstance( hInstance ) ){
  22.         MessageBox( NULL, "アプリケーションの初期化に失敗しました。", "LayeredWindow", MB_OK|MB_ICONHAND );
  23.         return FALSE;
  24.     }
  25.  
  26.     MSG msg;
  27.     while( GetMessage( &msg, NULL, 0, 0 ) ){
  28.         TranslateMessage( &msg );    // Translates virtual key codes.
  29.         DispatchMessage( &msg );    // Dispatches message to window.
  30.     }
  31.     ExitInstance();
  32.     return msg.wParam;    // Returns the value from PostQuitMessage.
  33. }
  34.  
  35. BOOL InitInstance( HINSTANCE hInstance )
  36. {
  37.     palphaWnd = new AlphaWindow( hInstance );
  38.     HWND hWnd = palphaWnd->Create();
  39.     return hWnd?TRUE:FALSE;
  40. }
  41.  
  42. void ExitInstance()
  43. {
  44.     if( palphaWnd ) delete palphaWnd;
  45. }
  46.  
  47. LONG APIENTRY MainWndProc( HWND hWnd, UINT message, UINT wParam, LONG lParam )
  48. {
  49.     if( message==WM_NCCREATE ){
  50.         CREATESTRUCT *pcs = (CREATESTRUCT*)lParam;
  51.         SetWindowLong( hWnd, GWL_USERDATA, (LONG)pcs->lpCreateParams );
  52.     }
  53.     AlphaWindow *pWnd = (AlphaWindow*)GetWindowLong( hWnd, GWL_USERDATA );
  54.     if( pWnd==NULL ){
  55.         return DefWindowProc( hWnd, message, wParam, lParam );
  56.     } else {
  57.         return pWnd->WndProc( hWnd, message, wParam, lParam );
  58.     }
  59.     return 0;
  60. }
  61.  
  62. //////////////////////////////////////////////////////////////////////
  63. // AlphaWindow クラス
  64. //////////////////////////////////////////////////////////////////////
  65.  
  66. //////////////////////////////////////////////////////////////////////
  67. // 構築/消滅
  68. //////////////////////////////////////////////////////////////////////
  69.  
  70. AlphaWindow::AlphaWindow( HINSTANCE hInstance )
  71. {
  72.     WNDCLASSEX wc;
  73.     wc.cbSize = sizeof( WNDCLASSEX );
  74.     wc.style = 0;
  75.     wc.lpfnWndProc = (WNDPROC)MainWndProc;
  76.     wc.cbClsExtra = 0;
  77.     wc.cbWndExtra = 0;
  78.     wc.hInstance = hInstance;
  79.     wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  80.     wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  81.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  82.     wc.lpszMenuName = NULL;
  83.     wc.lpszClassName = szAppName;
  84.     wc.hIconSm = NULL;
  85.     RegisterClassEx( &wc );
  86.     m_hInstance = hInstance;
  87.  
  88.     m_hMenu = CreatePopupMenu();
  89.     MENUITEMINFO mi;
  90.     mi.cbSize = sizeof(MENUITEMINFO);
  91.     mi.fMask = MIIM_ID|MIIM_TYPE;
  92.     mi.fType = MFT_STRING;
  93.     mi.wID = MM_QUIT;
  94.     mi.dwTypeData = "終了";
  95.     mi.cch = strlen( mi.dwTypeData );
  96.     InsertMenuItem( m_hMenu, 0, TRUE, &mi );
  97.  
  98.     HBITMAP hBitmap = LoadBitmap( m_hInstance, MAKEINTRESOURCE(IDB_RGB) );
  99.     HBITMAP hAlpha = LoadBitmap( m_hInstance, MAKEINTRESOURCE(IDB_ALPHA) );
  100.     m_hBitmap = CreateAlphaContainedBitmap( hBitmap, hAlpha );
  101.     HDC hDC = GetDC( NULL );
  102.     m_hDC = CreateCompatibleDC( hDC );
  103.     ReleaseDC( NULL, hDC );
  104.     SelectObject( m_hDC, m_hBitmap );
  105.     m_nWidth = 512;
  106.     m_nHeight = 256;
  107. }
  108.  
  109. // Alpha付きのビットマップを作成
  110. // 入力    hBitmap    カラービットマップ
  111. //        hAlpha    アルファビットマップ(R成分)
  112. // 出力    32ビットビットマップ
  113. HBITMAP AlphaWindow::CreateAlphaContainedBitmap( HBITMAP hBitmap, HBITMAP hAlpha )
  114. {
  115.     BITMAPINFOHEADER *pbmih1 = (BITMAPINFOHEADER*)GlobalAlloc( GPTR, sizeof(BITMAPINFOHEADER) );
  116.     memset( pbmih1, 0, sizeof(BITMAPINFOHEADER) );
  117.     pbmih1->biSize = sizeof(BITMAPINFOHEADER);
  118.     HDC hDC = GetDC( NULL );
  119.     GetDIBits( hDC, hBitmap, 0, 0, NULL, (BITMAPINFO*)pbmih1, DIB_RGB_COLORS );
  120.     pbmih1->biBitCount = 24;
  121.     pbmih1->biCompression = BI_RGB;
  122.     pbmih1->biXPelsPerMeter = pbmih1->biYPelsPerMeter = 0;
  123.     pbmih1->biClrUsed = pbmih1->biClrImportant = 0;
  124.  
  125.     BITMAPINFOHEADER *pbmih2 = (BITMAPINFOHEADER*)GlobalAlloc( GPTR, sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256 );
  126.     memset( pbmih2, 0, sizeof(BITMAPINFOHEADER) );
  127.     pbmih2->biSize = sizeof(BITMAPINFOHEADER);
  128.     GetDIBits( hDC, hAlpha, 0, 0, NULL, (BITMAPINFO*)pbmih2, DIB_RGB_COLORS );
  129.     pbmih2->biBitCount = 8;
  130.     pbmih2->biCompression = BI_RGB;
  131.     pbmih2->biXPelsPerMeter = pbmih2->biYPelsPerMeter = 0;
  132.     pbmih2->biClrUsed = pbmih2->biClrImportant = 256;
  133.     RGBQUAD *rgb = (RGBQUAD*)(pbmih2+1);
  134.  
  135.     BITMAPINFOHEADER *pbmih3 = (BITMAPINFOHEADER*)GlobalAlloc( GPTR, sizeof(BITMAPINFOHEADER) );
  136.     memset( pbmih3, 0, sizeof(BITMAPINFOHEADER) );
  137.     pbmih3->biSize = sizeof(BITMAPINFOHEADER);
  138.     pbmih3->biWidth = pbmih1->biWidth;
  139.     pbmih3->biHeight = pbmih1->biHeight;
  140.     pbmih3->biPlanes = 1;
  141.      pbmih3->biBitCount = 32;
  142.     pbmih3->biCompression = BI_RGB;
  143.     pbmih3->biSizeImage = 0;
  144.     pbmih3->biXPelsPerMeter = 0;
  145.     pbmih3->biYPelsPerMeter = 0;
  146.     pbmih3->biClrUsed = 0;
  147.     pbmih3->biClrImportant = 0;
  148.  
  149.     int line1 = (pbmih1->biWidth*3+3)/4*4;
  150.     LPBYTE lpvBits1 = (LPBYTE)GlobalAlloc( GPTR, line1*pbmih1->biHeight );
  151.     GetDIBits( hDC, hBitmap, 0, pbmih1->biHeight, lpvBits1, (BITMAPINFO*)pbmih1, DIB_RGB_COLORS );
  152.     int line2 = (pbmih2->biWidth+3)/4*4;
  153.     LPBYTE lpvBits2 = (LPBYTE)GlobalAlloc( GPTR, line2*pbmih2->biHeight );
  154.     GetDIBits( hDC, hAlpha, 0, pbmih2->biHeight, lpvBits2, (BITMAPINFO*)pbmih2, DIB_RGB_COLORS );
  155.  
  156.     LPBYTE pvBits;
  157.     HBITMAP hNewBitmap = CreateDIBSection( hDC, (BITMAPINFO*)pbmih3, DIB_RGB_COLORS, (void**)&pvBits, NULL, 0 );
  158.     LPBYTE p1 = lpvBits1, p2 = lpvBits2, p3 = pvBits;
  159.     for( int y=0; y<pbmih3->biHeight; y++ ){
  160.         for( int x=0; x<pbmih3->biWidth; x++ ){
  161.             *(p3++) = *(p1++);
  162.             *(p3++) = *(p1++);
  163.             *(p3++) = *(p1++);
  164.             *(p3++) = rgb[*(p2++)].rgbRed;
  165.         }
  166.         p1 += line1-pbmih3->biWidth*3;
  167.         p2 += line2-pbmih3->biWidth;
  168.     }
  169.     ReleaseDC( NULL, hDC );
  170.  
  171.     GlobalFree( lpvBits1 );
  172.     GlobalFree( lpvBits2 );
  173.     GlobalFree( pbmih1 );
  174.     GlobalFree( pbmih2 );
  175.     GlobalFree( pbmih3 );
  176.     return hNewBitmap;
  177. }
  178.  
  179. AlphaWindow::~AlphaWindow()
  180. {
  181.     DeleteDC( m_hDC );
  182.     DeleteObject( m_hBitmap );
  183.     DestroyMenu( m_hMenu );
  184. }
  185.  
  186. HWND AlphaWindow::Create()
  187. {
  188.     CREATESTRUCT cs;
  189.     cs.lpCreateParams = this;
  190.     cs.hInstance = m_hInstance;
  191.     cs.hMenu = NULL;
  192.     cs.hwndParent = NULL;
  193.     cs.cy = m_nHeight;
  194.     cs.cx = m_nWidth;
  195.     cs.y = CW_USEDEFAULT;
  196.     cs.x = CW_USEDEFAULT;
  197.     cs.style = WS_POPUP|WS_VISIBLE;
  198.     cs.lpszName = szTitle;
  199.     cs.lpszClass = szAppName;
  200.     cs.dwExStyle = WS_EX_TRANSPARENT;
  201.     m_hWnd = CreateWindowEx( cs.dwExStyle, cs.lpszClass, cs.lpszName, cs.style,
  202.         cs.x, cs.y, cs.cx, cs.cy, cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams );
  203.     return m_hWnd;
  204. }
  205.  
  206. LRESULT AlphaWindow::WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
  207. {
  208.     switch( message ){
  209.     case WM_CREATE:
  210.         OnCreate( hWnd );
  211.         return 0;
  212.     case WM_DESTROY:
  213.         OnDestroy();
  214.         PostQuitMessage( 0 );
  215.         return 0;
  216.     case WM_ERASEBKGND:
  217.         return 0;
  218.     case WM_PAINT:
  219.         OnPaint( (HDC)wParam );
  220.         return 0;
  221.     case WM_NCHITTEST:
  222.         return OnNcHitTest( LOWORD(lParam), HIWORD(lParam) );
  223.     case WM_NCRBUTTONDOWN:
  224.         OnNcRButtonDown( wParam, MAKEPOINTS(lParam) );
  225.         return 0;
  226.     case WM_COMMAND:
  227.         switch( LOWORD(wParam) ){
  228.         case MM_QUIT:
  229.             DestroyWindow( m_hWnd );
  230.             return 0;
  231.         }
  232.         break;
  233.     }
  234.     return DefWindowProc( hWnd, message, wParam, lParam );
  235. }
  236.  
  237. void AlphaWindow::OnCreate( HWND hWnd )
  238. {
  239. }
  240.  
  241. void AlphaWindow::OnDestroy()
  242. {
  243. }
  244.  
  245. void AlphaWindow::OnPaint( HDC hDC )
  246. {
  247.     PAINTSTRUCT ps;
  248.     hDC = BeginPaint( m_hWnd, &ps );
  249.     BLENDFUNCTION blend;
  250.     blend.BlendOp = AC_SRC_OVER;
  251.     blend.BlendFlags = 0;
  252.     blend.SourceConstantAlpha = 255;
  253.     blend.AlphaFormat = AC_SRC_ALPHA;
  254.     AlphaBlend( hDC, 0, 0, m_nWidth, m_nHeight, m_hDC, 0, 0, m_nWidth, m_nHeight, blend );
  255. //    BitBlt( hDC, 0, 0, m_nWidth, m_nHeight, m_hDC, 0, 0, SRCCOPY );
  256.     EndPaint( m_hWnd, &ps );
  257. }
  258.  
  259. int AlphaWindow::OnNcHitTest( int xPos, int yPos )
  260. {
  261.     return HTCAPTION;
  262. }
  263.  
  264. void AlphaWindow::OnNcRButtonDown( int nHittest, POINTS pts )
  265. {
  266.     TrackPopupMenu( m_hMenu, TPM_LEFTALIGN|TPM_TOPALIGN, pts.x, pts.y, 0, m_hWnd, NULL );
  267. }
  268.